﻿<%@ WebHandler Language="C#" Class="DeliverList" %>
using System;
using System.Web;
using System.Text;
using System.Data;
using Chenxin.Lib.Utility;
using Chenxin.Lib.Business;
using Chenxin.Lib.Business.BLL;
using Chenxin.Lib.Hr;

public class DeliverList : AjaxHandlerBase
{
    
    /// <summary>
    /// 基本权限项
    /// </summary>
    public DeliverList()
    {
        //意向书下发
        htTypePermit.Add("letterofintentdeliver", Constants.Permit_LetterOfIntentDeliver);
    }
    
    override public void RequestProcess(HttpContext context)
    {
        string strOp = context.Request["op"];
        string strResponse = "";
        switch (strOp.ToLower())
        {
            //查询
            case "search":
                strResponse = Search(context);
                break;
            //下发
            case "deliver":
                strResponse = Deliver(context);
                break;
            default:
                break;
        }
        context.Response.Write(strResponse);
    }

    //*******************************************
    //功能概要       ：获得意向书下发信息
    //参数説明       ：context 请求的上下文
    //返回值説明     ：无
    //功能詳細       ：获得意向书下发信息
    //作成者         ：zou.xiaochun
    //作成日         ：2010-08-31
    //*******************************************
    private string Search(HttpContext context)
    {
        //返回数据
        string strRet = string.Empty;
        
        try
        {
            StringBuilder sb = new StringBuilder();
            string idList = context.Request["idList"].Replace(",", "','");
            sb.AppendFormat(" IntentionId in ('{0}')", idList);
            //条件
            string strWhere = sb.ToString();
            string strOrderField = " t_Intention_BaseInfo.CreateDate desc ";
            //页码
            int strPageIndex = (string.IsNullOrEmpty(context.Request["pageindex"])) ? 1 : int.Parse(context.Request["pageindex"]) + 1;//页码
            //页显示行数
            int strPageSize = (string.IsNullOrEmpty(context.Request["pagesize"])) ? 30 : int.Parse(context.Request["pagesize"]);//页记录数
            //欲选择字段列表 
            string FieldList = "IntentionId,IntentionCode,BusinessType,IntentionType,CreateDate";
            //子查询欲选择字段列表
            string TableFieldList = "IntentionId,IntentionCode,BusinessType,IntentionType,CreateDate";
            //所要查询的表名
            string TableName = "t_Intention_BaseInfo ";
            //根据sql执行查询
            DataSet ds = Chenxin.Lib.Business.DAL.t_Intention_BaseInfo.GetList(strPageIndex, strPageSize, strOrderField, TableName, strWhere, FieldList, TableFieldList, true);
            
            Constants.Pagination cp = GetJson(ds);
            strRet = string.Format("{{success:1,msg:'',data:{{count:{0},html:'{1}'}}}}", cp.count, cp.data);
            return strRet;
        }
        catch (Exception ex)
        {
            strRet = retExecResult(Constants.ExecResult.Error, ex.Message, "");
            return strRet;
        }
    }
    
    //*******************************************
    //功能概要       ：转化为翻页结构体格式
    //参数説明       ：dtData 要转化的原始数据
    //               ：intShowRows 一页所要显示的条数
    //               ：intCurPage 当前的页码
    //               ：intTotal 数据总条数
    //返回值説明     ：翻页结构体
    //功能詳細       ：转化为翻页结构体格式
    //作成者         ：zou.xiaochu
    //作成日         ：2010-08-31 
    //*******************************************
    public Constants.Pagination GetJson(System.Data.DataSet dsData)
    {
        Constants.Pagination stuPag = new Constants.Pagination();
        System.Data.DataRow row;
        DateTime dtTempTime;

        System.Text.StringBuilder strData = new System.Text.StringBuilder();

        for (int idxRow = 0; idxRow < dsData.Tables[0].Rows.Count; idxRow++)
        {
            row = dsData.Tables[0].Rows[idxRow];

            strData.AppendFormat("<dl id=\"{0}\">", row["IntentionId"].ToString());
            strData.AppendFormat("<dd style=\"display: none;\">{0}</dd>", row["IntentionId"].ToString());
            strData.AppendFormat("<dd>{0}</dd>", row["IntentionCode"].ToString());
            if (!string.IsNullOrEmpty(row["BusinessType"].ToString()))
            {
                switch (int.Parse(row["BusinessType"].ToString()))
                {
                    case 10:
                        strData.AppendFormat("<dd>{0}</dd>", "买卖");
                        break;
                    case 20:
                        strData.AppendFormat("<dd>{0}</dd>", "租赁");
                        break;
                }
            }
            else
            {
                strData.Append("<dd></dd>");
            }
            if (!string.IsNullOrEmpty(row["IntentionType"].ToString()))
            {
                switch (int.Parse(row["IntentionType"].ToString()))
                {
                    case 10:
                        strData.AppendFormat("<dd>{0}</dd>", "买卖协议");
                        break;
                    case 20:
                        strData.AppendFormat("<dd>{0}</dd>", "居间协议");
                        break;
                }
            }
            else
            {
                strData.Append("<dd></dd>");
            }
            if (!string.IsNullOrEmpty(row["CreateDate"].ToString()))
            {
                dtTempTime = (DateTime)row["CreateDate"];
                strData.AppendFormat("<dd>{0}</dd>", dtTempTime.Year.ToString().PadLeft(4, '0') + "-" +
                                                     dtTempTime.Month.ToString().PadLeft(2, '0') + "-" +
                                                     dtTempTime.Day.ToString().PadLeft(2, '0'));
            }
            else
            {
                strData.Append("<dd></dd>");
            }
            strData.Append("</dl>");
        }

        stuPag.count = Convert.ToInt32(dsData.Tables[1].Rows[0][0]);
        stuPag.data = strData.ToString();
        return stuPag;
    }

    //*******************************************
    //功能概要       ：意向书编号下发
    //参数説明       ：context 请求上下文
    //返回值説明     ：程序处理信息提示
    //功能詳細       ：意向书编号下发
    //作成者         ：zou.xiaochun
    //作成日         ：2010-08-31
    //*******************************************
    private string Deliver(HttpContext context)
    {
        //意向书信息
        string ListInfo = context.Request.Params["chk_row"];
        
        //意向书主表
        t_Intention_BaseInfo bllIntention = new t_Intention_BaseInfo();

        //意向日志
        t_Intention_DeliverLogs bllLogs = new t_Intention_DeliverLogs();

        //意向书信息格式化
        Chenxin.Lib.Business.Entity.t_Intention_BaseInfo entIntention = new Chenxin.Lib.Business.Entity.t_Intention_BaseInfo();
        Chenxin.Lib.Business.Entity.t_Intention_DeliverLogs entDeliverLogs = new Chenxin.Lib.Business.Entity.t_Intention_DeliverLogs();
        entIntention.BelongCityId = new Guid(context.Request.Params["cityid"]);
        entIntention.BelongCityName = context.Request.Params["city"].ToString();
        entIntention.BelongCompanyId = new Guid(context.Request.Params["companyid"]);
        entIntention.BelongCompany = context.Request.Params["company"].ToString();
        entIntention.BelongDepartId = new Guid(context.Request.Params["departid"]);
        entIntention.BelongDepart = context.Request.Params["depart"].ToString();
        entIntention.BelongEmpId = new Guid(context.Request.Params["userid"]);
        entIntention.BelongEmp = context.Request.Params["user"].ToString();
        entDeliverLogs.ReceiveCityId = entIntention.BelongCityId;
        entDeliverLogs.ReceiveCity = entIntention.BelongCityName;
        entDeliverLogs.ReceiveCompanyId = entIntention.BelongCompanyId;
        entDeliverLogs.ReceiveCompany = entIntention.BelongCompany;
        entDeliverLogs.ReceiveDepartId = entIntention.BelongDepartId;
        entDeliverLogs.ReceiveDepart = entIntention.BelongDepart;
        entDeliverLogs.ReceiveEmpId = entIntention.BelongEmpId;
        entDeliverLogs.ReceiveEmp = entIntention.BelongEmp;

        string[] modelArray = ListInfo.Split(',');
        for (int i = 0; i < modelArray.Length; i++)
        {
            entIntention.IntentionId = new Guid(modelArray[i]); 
            entIntention = CompleteIntention(entIntention);
            bllIntention.Update(entIntention);
            entDeliverLogs.IntentionId = new Guid(modelArray[i]);
            entDeliverLogs = CompleteDeliverLogs(entDeliverLogs);
            entDeliverLogs.IntentionCode = entIntention.IntentionCode;
            entDeliverLogs.Status = 10;
            bllLogs.Add(entDeliverLogs);
        }
        return retExecResult(Constants.ExecResult.Succ, Resources.Message.Msg_Intention_Deliver, "");
    }

    //*******************************************
    //功能概要       ：填充预审信息字段
    //参数説明       ：context 请求上下文
    //返回值説明     ：预审信息实体
    //功能詳細       ：填充预审信息字段
    //作成者         ：zou.xiaochun
    //作成日         ：2010-07-09
    //*******************************************
    private Chenxin.Lib.Business.Entity.t_Intention_BaseInfo CompleteIntention(Chenxin.Lib.Business.Entity.t_Intention_BaseInfo entIntention)
    {
        Chenxin.Lib.Business.BLL.t_Intention_BaseInfo bllIntention = new Chenxin.Lib.Business.BLL.t_Intention_BaseInfo();
        Chenxin.Lib.Business.Entity.t_Intention_BaseInfo entOlIntention = bllIntention.GetModel(entIntention.IntentionId);
        entIntention.IntentionCode = entOlIntention.IntentionCode;
        entIntention.BusinessType = entOlIntention.BusinessType;
        entIntention.IntentionType = entOlIntention.IntentionType;
        entIntention.CreateDate = entOlIntention.CreateDate;
        entIntention.CreateEmpId = entOlIntention.CreateEmpId;
        entIntention.CreateEmp = entOlIntention.CreateEmp;
        entIntention.UpdateEmpId = CurrentUserInfo.UserId;
        entIntention.UpdateEmp = CurrentUserInfo.UserName;
        entIntention.UpdateTime = DateTime.Today;
        entIntention.DeliverTime = DateTime.Today;
        entIntention.Status = -200;
        return entIntention;
    }

    //*******************************************
    //功能概要       ：填充预审信息字段
    //参数説明       ：context 请求上下文
    //返回值説明     ：预审信息实体
    //功能詳細       ：填充预审信息字段
    //作成者         ：zou.xiaochun
    //作成日         ：2010-07-09
    //*******************************************
    private Chenxin.Lib.Business.Entity.t_Intention_DeliverLogs CompleteDeliverLogs(Chenxin.Lib.Business.Entity.t_Intention_DeliverLogs entDeliverLogs)
    {
        entDeliverLogs.RecordId = Guid.NewGuid();
        entDeliverLogs.DeliverTime = DateTime.Today;
        entDeliverLogs.DeliverEmpId = CurrentUserInfo.UserId;
        entDeliverLogs.DeliverEmp = CurrentUserInfo.UserName;
        entDeliverLogs.DeliverDepartId = CurrentUserInfo.DepartId;
        entDeliverLogs.DeliverDepart = CurrentUserInfo.DepartName;
        entDeliverLogs.DeliverCompanyId = CurrentUserInfo.CompanyId;
        entDeliverLogs.DeliverCompany = CurrentUserInfo.CompanyName;
        entDeliverLogs.DeliverCityId= CurrentUserInfo.CityId;
        entDeliverLogs.DeliverCity = CurrentUserInfo.CityName;
        return entDeliverLogs;
    } 
}